<template>
  <div class="role-management">
    <el-card>
      <template #header>
        <div class="card-header">
          <span>角色管理</span>
          <el-button type="primary" @click="addRole">添加角色</el-button>
        </div>
      </template>

      <el-table :data="roles" style="width: 100%" v-loading="loading">
        <el-table-column prop="roleId" label="角色ID" width="150" />
        <el-table-column prop="roleName" label="角色名称" width="200" />
        <el-table-column prop="roleDescription" label="角色描述" />
        <el-table-column label="操作" width="200">
          <template #default="scope">
            <el-button size="small" @click="editRole(scope.row)">编辑</el-button>
            <el-button size="small" type="danger" @click="deleteRole(scope.row.roleId)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 角色编辑对话框 -->
      <el-dialog v-model="dialogVisible" :title="dialogTitle" width="500px">
        <el-form :model="currentRole" label-width="100px">
          <el-form-item label="角色ID">
            <el-input v-model="currentRole.roleId" :disabled="!!currentRole.roleId" />
          </el-form-item>
          <el-form-item label="角色名称">
            <el-input v-model="currentRole.roleName" />
          </el-form-item>
          <el-form-item label="角色描述">
            <el-input v-model="currentRole.roleDescription" type="textarea" />
          </el-form-item>
        </el-form>
        <template #footer>
          <span class="dialog-footer">
            <el-button @click="dialogVisible = false">取消</el-button>
            <el-button type="primary" @click="saveRole">保存</el-button>
          </span>
        </template>
      </el-dialog>
    </el-card>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { getRoles, createRole, updateRole, deleteRole as deleteRoleApi } from '@/api/role'

const roles = ref([])
const loading = ref(false)
const dialogVisible = ref(false)
const currentRole = ref({})
const dialogTitle = ref('')

// 加载角色列表
const loadRoles = async () => {
  loading.value = true
  try {
    const response = await getRoles()
    roles.value = response.data
  } catch (error) {
    ElMessage.error('加载角色列表失败')
  } finally {
    loading.value = false
  }
}

// 添加角色
const addRole = () => {
  currentRole.value = { roleId: '', roleName: '', roleDescription: '' }
  dialogTitle.value = '添加角色'
  dialogVisible.value = true
}

// 编辑角色
const editRole = (role) => {
  currentRole.value = { ...role }
  dialogTitle.value = '编辑角色'
  dialogVisible.value = true
}

// 保存角色
const saveRole = async () => {
  try {
    if (currentRole.value.roleId) {
      // 更新现有角色
      await updateRole(currentRole.value.roleId, currentRole.value)
      ElMessage.success('角色更新成功')
    } else {
      // 创建新角色
      await createRole(currentRole.value)
      ElMessage.success('角色创建成功')
    }
    
    dialogVisible.value = false
    loadRoles() // 重新加载角色列表
  } catch (error) {
    ElMessage.error('保存角色失败')
  }
}

// 删除角色
const deleteRole = async (roleId) => {
  try {
    await ElMessageBox.confirm(
      '确认删除这个角色吗？',
      '警告',
      {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }
    )
    
    await deleteRoleApi(roleId)
    ElMessage.success('角色删除成功')
    loadRoles() // 重新加载角色列表
  } catch (error) {
    if (error !== 'cancel') {
      ElMessage.error('删除角色失败')
    }
  }
}

onMounted(() => {
  loadRoles()
})
</script>

<style scoped>
.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.role-management {
  padding: 20px;
}
</style>